package com.my.chapter10;

import net.mindView.innerclasses.Controller;
import net.mindView.innerclasses.Event;

public class practice25 {
    public static void main(String[] args) {
        GreenhouseControlsP25 gc = new GreenhouseControlsP25();
        gc.addEvent(gc.new Bell(900));
        Event[] eventList = {
                gc.new ThermostatNight(0),
                gc.new LightOn(200),
                gc.new LightOff(400),
                gc.new WaterOn(600),
                gc.new WaterOff(800),
                gc.new FanOn(1000),
                gc.new FanOff(1200),
                gc.new SprinklerOn(1400),
                gc.new SprinklerOff(1600),
                gc.new ThermostatDay(1800)
        };
        gc.addEvent(gc.new Restart(2000,eventList));

        gc.addEvent(new GreenhouseControls.Terminate(2000));

        gc.run();
    }
}

class GreenhouseControlsP25 extends Controller {
    private boolean light = false;
    public class LightOn extends Event {

        public LightOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = true;
        }

        @Override
        public String toString() {
            return "Light is on";
        }
    }
    public class LightOff extends Event{

        public LightOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = false;
        }

        @Override
        public String toString() {
            return "Light is off";
        }
    }

    private boolean water = false;
    public class WaterOn extends Event{

        public WaterOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            water = true;
        }

        @Override
        public String toString() {
            return "Greenhouse water is on";
        }
    }
    public class WaterOff extends Event{

        public WaterOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            water = false;
        }

        @Override
        public String toString() {
            return "Greenhouse water is off";
        }
    }

    private String thermostat = "Day";
    public class ThermostatNight extends Event{

        public ThermostatNight(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            thermostat = "Night";
        }

        @Override
        public String toString() {
            return "Thermostat on night setting";
        }
    }
    public class ThermostatDay extends Event{

        public ThermostatDay(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            thermostat = "Day";
        }

        @Override
        public String toString() {
            return "Thermostat on day setting";
        }
    }

    private boolean fan = false;
    public class FanOn extends Event{

        public FanOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            fan = true;
        }

        @Override
        public String toString() {
            return "Fan is on";
        }
    }
    public class FanOff extends Event{

        public FanOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            fan = false;
        }

        @Override
        public String toString() {
            return "Fan is off";
        }
    }

    private boolean sprinkler = false;
    public class SprinklerOn extends Event{

        public SprinklerOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            sprinkler = true;
        }

        @Override
        public String toString() {
            return "Sprinkler is on";
        }
    }
    public class SprinklerOff extends Event{

        public SprinklerOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            sprinkler = false;
        }

        @Override
        public String toString() {
            return "Sprinkler is off";
        }
    }

    public class Bell extends Event{

        public Bell(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            addEvent(new Bell(delayTime));
        }

        @Override
        public String toString() {
            return "Bing!";
        }
    }

    public class Restart extends Event{
        private Event[] eventList;

        public Restart(long delayTime,Event[] eventList) {
            super(delayTime);
            this.eventList = eventList;
            for(Event e:eventList){
                addEvent(e);
            }
        }

        @Override
        public void action() {
            for(Event e:eventList){
                e.start();
                addEvent(e);
            }
            start();
            addEvent(this);
        }

        @Override
        public String toString() {
            return "Restarting system";
        }
    }

    public static class Terminate extends Event{

        public Terminate(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            System.exit(0);
        }

        @Override
        public String toString() {
            return "Terminating";
        }
    }

}
